layui.config({
	base: "js/"
}).use(['form', 'layer', 'jquery', 'laypage'], function() {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		$ = layui.jquery;

	var that
	var arr
	var boxData = '';
	$.get("/weixin/open/getBoxList?caseNo="+$(".caseNo").val(), function(data) {
		//正常加载信息
		boxData = data;
		if(window.sessionStorage.getItem("addbox")) {
			var addbox = window.sessionStorage.getItem("addbox");
			boxData = JSON.parse(addbox).concat(boxData);
		}
		$(".box").html(renderDate(boxData));//显示页面
		if(boxData.adminFlag == 1){
			$('<a href="#" class="logo">恒铸智能柜管理平台</a>').appendTo(".log");//显示页面
			$('<input class="layui-btn3 layui-btn" type="button"  id="clear" value="清除" />').appendTo(".box");//显示页面
			$('<input class="layui-btn2" type="button"  id="opens" value="一键开启" />').appendTo(".box");//显示页面
			$('<input class="layui-btn1 layui-btn" type="button"  id="open" value="开启" />').appendTo(".box");//显示页面
		}else {
			$('<a href="#" class="logo">恒铸智能柜用户中心</a>').appendTo(".log");//显示页面
			$('<input class="layui-btn layui-btn" type="button"  id="open" value="开启" />').appendTo(".box");//显示页面
		}
		form.render();
		
	})
	function renderDate(data){
		var dataHtml = '';
			 currData = data.list;
		if(currData.length != 0){
			for(var i=0;i<currData.length;i++){
					if(0 == currData[i].sign){
						dataHtml += '<li id="'+currData[i].caseNo+'"class="open" >'+currData[i].boxNum+'</li>';
					}else if(2 == currData[i].sign){
						dataHtml += '<li id="'+currData[i].caseNo+'"class="openedOut" style="background-color:#FF4040;" >'+
						currData[i].boxNum+'</li>';
					}else{
						dataHtml += '<li id="'+currData[i].caseNo+'"class="opened" style="background-color:#FFB800;" >'+
							currData[i].boxNum+'</li>';
					}
			}
		}else{
			dataHtml = '<tr><td colspan="8">暂无可使用储物柜，请稍后重试</td></tr>';
		}
	    return dataHtml;
	}
	
	
	
	//操作  dblclick双击
	var caseNo
	$("body").on("click", ".open", function() {
		that = $(this);
		arr="";
		$(".open").val(0);
		$(".open").css("background-color", "white");
		$(".opened").css("background-color", "#FFB800");
		$(".openedOut").css("background-color", "#FF4040");
		if(that.val() == 0) {
			that.val(1);
			caseNo = that.attr('id')
			that.css("background-color", "#009688");
			arr = that.text(); //添加id到数组
		} 
	})
	$("body").on("click", ".opened", function() { 
		arr="";
		that = $(this);
		$(".opened").val(0);
		$(".opened").css("background-color", "#FFB800");
		$(".open").css("background-color", "white");
		$(".openedOut").css("background-color", "#FF4040");
		if(that.val() == 0) {
			that.val(1);
			caseNo = that.attr('id')
			that.css("background-color", "#009688");
			arr = that.text(); //添加id到数组
		}
	})
	$("body").on("click", ".openedOut", function() { 
		arr="";
		that = $(this);
		$(".openedOut").val(0);
		$(".opened").css("background-color", "#FFB800");
		$(".open").css("background-color", "white");
		$(".openedOut").css("background-color", "#009688");
		if(that.val() == 0) {
			that.val(1);
			caseNo = that.attr('id')
			that.css("background-color", "#009688");
			arr = that.text(); //添加id到数组
		}
	})
	$("body").on("click", "#open", function() {
			if(null==arr){
				layer.msg("没有选中！！");
			}else{
				layer.alert('你确定要开启"'+arr+'"柜子？', {
					  skin: 'layui-layer-molv' //样式类名
						 , btn: ['确定', '取消'],
							yes: function(index) {
								if(boxData.adminFlag != 1){
									that.remove();
								}
								$.get("/weixin/open/openDoor?caseNo="+caseNo+"&boxNum="+arr,function(data){
									  layer.alert(data.message, {
										    skin: 'layui-layer-molv'
										    ,closeBtn: 0
										    , btn: ['确定', '取消']
											,yes: function(index) {
												if(data.flag == 1){//存
													if(boxData.adminFlag != 1){
														window.location.href="/weixin/open/please"; 
													}else{
														layer.close(index);
													}
												}else if(data.flag == 2){//取
													if(boxData.adminFlag != 1){
														window.location.href="/weixin/open/opened"; 
													}else{
														layer.close(index);
													}
												}else if(data.flag == 3){//失败
														layer.close(index);
												}else if(data.flag == 4){//管理员开箱
													if(boxData.adminFlag != 1){
														window.location.href="/weixin/open/adminOpened"; 
													}else{
														layer.close(index);
													}
												}
											}
										  });
								})
								arr=null;
							}
				})   
			}
		})
				$("body").on("click","#opens",function() {
					layer.alert("确定开启全部吗？", {
						skin : 'layui-layer-molv',
						closeBtn : 0,
						btn : [ '确定', '取消' ],
						yes : function(index) {
							layer.close(index);
							$.get("/weixin/open/oneKeyOpen?caseNo="+ $(".caseNo").val(),function(data){
								alert(data.message)
							})
						}
					})
				})
				
				
		$("body").on("click", "#clear", function() {
			if(null==arr){
				layer.msg("没有选中！！");
			}else{
				layer.alert('清除"'+arr+'"柜子前请检查是否没有物品了，<br>以免造成不必要的损失，若有物品请妥善保管！！！', {
					  skin: 'layui-layer-molv' //样式类名
						 , btn: ['确定', '取消'],
							yes: function(index) {
								that.css("background-color", "white");
								that.val(0);
								that.removeClass();
								that.addClass("open")
								$.get("/weixin/open/clear?caseNo="+caseNo+"&boxNum="+arr,function(data){
									  layer.alert(data.message, {
										    skin: 'layui-layer-molv'
										    ,closeBtn: 0
										    , btn: ['确定', '取消']
											,yes: function(index) {
													layer.close(index);
											}
										  });
								})
							}
				})   
			}
		})
				
				
})
